package rmi;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import servlet.MastermindServlet;
import bean.Message;

public class ComManager extends MastermindServlet {

	
	
	public Message comRmi (Message mOut){
			Message mIn = null; 
		try {
			
			Registry annuaire = LocateRegistry.getRegistry(Integer.parseInt(config.getProperty("rmiport")));
			RmiServer reference;
			reference = (RmiServer)annuaire.lookup(config.getProperty("urlRmi"));
		
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			XMLEncoder encoder = new XMLEncoder(baos);
			encoder.writeObject(mOut);
			encoder.flush();
			encoder.close();
			
			String sIn = reference.daoCom(baos.toString());
			
			ByteArrayInputStream bais = new ByteArrayInputStream(sIn.getBytes());
			XMLDecoder decoder =new XMLDecoder(bais);
			mIn = (Message)decoder.readObject();
						
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mIn;
	}
}
